package com.tul.exercise.base.swing;

import javax.swing.*;
import java.awt.*;

/**
 * @Description 《java编程思想》第22章 示例5：
 *
 * 显示框架
 **/
public class SwingConsole {
    // 得到显示器屏幕的宽高
    public int windowsWedth = Toolkit.getDefaultToolkit().getScreenSize().width;
    public int windowsheight = Toolkit.getDefaultToolkit().getScreenSize().height;


    public static void run(final JFrame f, final int width, final int height) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                // 得到显示器屏幕的宽高
                int windowsWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
                int windowsHeight = Toolkit.getDefaultToolkit().getScreenSize().height;

                f.setTitle(f.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setSize(width, height);
                f.setBounds((windowsWidth - width) / 2, (windowsHeight - height) / 2, width, height);
                f.setVisible(true);
            }
        });
    }

    /**
     *
     * @param f JFrame类对象
     * @param title 标题
     * @param width 宽度
     * @param height 高度
     */
    public static void run(final JFrame f,String title,  final int width, final int height) {


        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                // 得到显示器屏幕的宽高
                int windowsWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
                int windowsHeight = Toolkit.getDefaultToolkit().getScreenSize().height;

                f.setTitle(title);
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setSize(width, height);
                f.setBounds((windowsWidth - width) / 2, (windowsHeight - height) / 2, width, height);
                f.setVisible(true);
            }
        });
    }
}
